(function() {
  var $shortcutName = $('#shortcut-name');
  var $shortcutPath = $('#shortcut-path');
  var $shortcutPathField = $('#shortcut-path-val');
    
  var sbi = window.ShortcutBaseInfo = {
    setup: function() {
      sbi.shortcutPathSetup();
    },

    shortcutPathSetup: function() {
      var paths = ShortcutPathStorage.getPaths();

      if (paths.length == 0) {
        var desktopFolder = Fs.desktopFolder;
        var quickLaunchFolder = Fs.quickLaunchFolder;
        var startMenuFolder = Fs.startMenuFolder;
        if (isWindowsPlatform()) {
          ShortcutPathStorage.addPath(i18n['start_menu'], startMenuFolder);
          ShortcutPathStorage.addPath(i18n['quick_launch'], quickLaunchFolder);
        }
        ShortcutPathStorage.addPath(i18n['desktop'], desktopFolder);
        ShortcutPathStorage.setDefaultPath(i18n['desktop']);
        paths = ShortcutPathStorage.getPaths();
      }

      // Init shortcut path field and droplist.
      var droplist = new Droplist($shortcutPath.get(), paths);
      droplist.setSelected = function(text, value) {
        value = value || text;
        sbi.setPath(value, text);
        ShortcutPathStorage.setDefaultPath(text);
      };

      $(document).on('click', function() {
        droplist.hide();
      });

      // Set last selected shortcut path.
      var defaultShortcutPath = ShortcutPathStorage.getDefaultPath();
      sbi.setPath(defaultShortcutPath[1], defaultShortcutPath[0]);

      var html = '<div id="custom-shortcut-path">' + i18n['custom_path']
        + '</div>';
      droplist.addItem(html, 'none', true);
      
      $('#custom-shortcut-path').on('click', function(e) {
        e.stopPropagation();
        var shortcutPath = Fs.selectPath(i18n['select_shortcut_path']);
        if (shortcutPath) {
          var path = ShortcutPathStorage.getPath(shortcutPath);
          var pathText = shortcutPath;
          if (!path) {
            ShortcutPathStorage.addPath(shortcutPath);
            if (droplist.length > 9) {
              droplist.remove(5);
            }
            droplist.addItem(shortcutPath);
          } else {
            pathText = path[0];
          }
          droplist.setSelected(pathText, shortcutPath);
          droplist.hide();
        }
      });
    },
    
    init: function() {
      $shortcutName.val('');
    },
    
    getName: function() {
      return $shortcutName.val();
    },
    
    setName: function(name) {
      $shortcutName.val(name);
    },

    focusName: function() {
      $shortcutName.get().focus();
    },

    validateName: function() {
      var name = sbi.getName();
      if (name == '') {
        Tip.alertField($shortcutName.get(), 'required_shortcut_name');
        sbi.focusName();
        return false;
      } else if (ShortcutSetting.isNewShortcut &&
                 ShortcutsStorage.isShortcutExistent(name)) {
        Tip.alertField($shortcutName.get(), 'shortcut_existent');
        sbi.focusName();
        return false;
      }
      return true;
    },

    getPath: function() {
      return $shortcutPathField.attr('data-path-value');
    },

    setPath: function(pathValue, pathText) {
      if (!pathText) {
        var path = ShortcutPathStorage.getPath(pathValue);
        if (path) {
          pathText = path[0];
        } else {
          pathText = pathValue;
        }
      }
      $shortcutPathField.text(pathText);
      $shortcutPathField.attr('data-path-value', pathValue);
    },

    validatePath: function() {
      var path = sbi.getPath();
      if (path == '' || !Fs.validatePath(path)) {
        Tip.alertField($shortcutPath.get(), 'invalid_shortcut_path');
        return false;
      }
      return true;
    },

    applyShortcutData: function(name, path) {
      sbi.setName(name);
      sbi.setPath(path);
    },

    generateShortcutData: function() {
      return {
        name: sbi.getName(),
        path: sbi.getPath()
      };
    }
  };
})();